<template>
  <div class="xtx-checkbox" @click="change">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <slot></slot>
  </div>
</template>
<script>
// import { ref, watch } from 'vue'
import { useVModel } from '@vueuse/core'
export default {
  name: 'XtxCheckbox',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  setup (props, { emit }) {
    // const checked = ref(false)
    // watch(() => props.modelValue, value => {
    //   checked.value = value
    //   console.log(checked.value)
    // }, {
    //   immediate: true
    // })
    // const change = () => {
    //   emit('update:modelValue', !checked.value)
    // }

    // 以下代码优化以上代码---s
    const checked = useVModel(props, 'modelValue', emit)
    const change = () => {
      const value = !checked.value
      checked.value = value
      emit('change', value)
    }
    // 以下代码优化以上代码---e
    return {
      checked,
      change
    }
  }
}
</script>
<style lang="less" scoped>
  .xtx-checkbox {
    display: inline-block;
    margin-right: 2px;
    .icon-checked {
      color: @xtxColor;
      ~ span {
        color: @xtxColor;
      }
    }
    i {
      position: relative;
      top: 1px;
    }
    span {
      margin-left: 2px;
    }
  }
</style>
